<?php

/**
 * @author Argel Cabalag
 * @copyright 2009
 */

class mcActions extends sfActions
{
	public function executeIndex(sfWebRequest $request)
  {


    $q = Doctrine_Query::create()
    ->from('Client c')
    ->orderBy('c.created_at DESC');
    $this->client_list = $q->execute();


  }
 public function executeSearch(sfWebRequest $request)
  {
	  $this->form = new SearchForm();

	  $s = $this->getRequestParameter('list');
	  $this->s = $s;
	  $query = '%'.$this->getRequestParameter('query').'%';

	  if($s == 'MC')
	  {
		  $m = Doctrine_Query::create()
		  ->from('Client c')
		  ->where('c.name LIKE ?', $query);
		  $this->list = $m->execute();
	  }
	  elseif($s == 'PX')
	  {

		  $mm = Doctrine_Query::create()
		      ->select('px.*, p.*')
              ->from('Passenger px, px.Person p')
              ->where('p.last_name LIKE ?', $query)
              ->orWhere('p.first_name LIKE ?', $query);

			//->from('Passenger p')
			//->where('EXISTS (SELECT px.* FROM Person px WHERE px.id = p.person_id and px.first_name LIKE ? $query )');

		  $this->list2 = $mm->execute();			
	  }	  
	}
	
	
	
	
	
  public function executeShow(sfWebRequest $request)
  {
    $this->client = Doctrine::getTable('Client')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->client);
    $cid = $this->getRequestParameter('id');
    $q = Doctrine_Query::create()
    ->from('Websites w')
    ->where('w.client_id ='.$cid.'');
    $this->web_list = $q->execute();

    $s = Doctrine_Query::create()
    ->from('Address w')
    ->where('w.client_id ='.$cid.'');
    $this->add_list = $s->execute();
    
    $t = Doctrine_Query::create()
    ->from('Phone p')
    ->where('p.client_id ='.$cid.'');
    $this->phone_list = $t->execute();
    $this->client_id = $cid;
  }
  public function executeNew(sfWebRequest $request)
  {
		$client_type = $this->getRequestParameter('Add_New_Client');
		$mc = new Client();
		$mc->setMc_No($this->computeMCNo());
		$mc->setAuthor($this->getUser()->getGuardUser()->getId());
		$mc->setClientType($client_type);

    $this->form = new MainClientForm($mc);

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));
		$mc = new Client();
		$mc->setAuthor($this->getUser()->getGuardUser()->getId());
    $this->form = new MainClientForm($mc);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($mainClient = Doctrine::getTable('Client')->find(array($request->getParameter('id'))), sprintf('Object client does not exist (%s).', array($request->getParameter('id'))));
		$this->client = $mainClient;
		    $this->client_id = $mainClient->getId();
    $this->form = new MainClientForm($mainClient);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($mainClient = Doctrine::getTable('Client')->find(array($request->getParameter('id'))), sprintf('Object client does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new MainClientForm($mainClient);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($mainClient = Doctrine::getTable('Client')->find(array($request->getParameter('id'))), sprintf('Object client does not exist (%s).', array($request->getParameter('id'))));
	  $this->client_id = $mainClient->getid();
    $mainClient->delete();

    $this->redirect('main/index');
  }


  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $mc = $form->save();

      $this->redirect('mc/edit?id='.$mc['id']);
    }
  }


	public function executeAddPhoneAjax(sfWebRequest $request)
  {
    $phone_index = $request->getParameter('phone_index');
    $this->forward404Unless($phone_index);

    $embedName = 'new_'.$phone_index; // the unique name to give this form
    $form = new MainClientForm();
    $form->addNewPhone($embedName);

    $this->form = $form['phones'][$embedName];
		$this->embedName = $embedName;
    $this->renderPartial('phone');

    return sfView::NONE;
  }

	public function executeAddAddressAjax(sfWebRequest $request)
  {
    $address_index = $request->getParameter('address_index');
    $this->forward404Unless($address_index);

    $embedName = 'new_'.$address_index; // the unique name to give this form
    $form = new MainClientForm();
    $form->addNewAddress($embedName);

    $this->form = $form['addresss'][$embedName];
		$this->embedName = $embedName;
    $this->renderPartial('address');

    return sfView::NONE;
  }


	public function computeMCNo()
	{
		$controlArray = array(0);
		$mcQuery = Doctrine_Query::create()
	    ->from('Client c')
			->orderBy('c.id DESC');
		$mc = $mcQuery->fetchOne();
		$amount = 0;
		if((isset($mc) && $mc != '')){
				$id = $mc->getid();
		//		$control_no = (int)substr($mc_no, 0, 4);
				
			/*	array_push($controlArray, $control_no);
				$this->logMessage('values'.$control_no);
			
			sort($controlArray);
			$index = count($controlArray);
				$this->logMessage('index'.$index);
			$highestControl = $controlArray[$index-1];
				$this->logMessage('control'.$highestControl);*/
		}
		$amount = $id +1;
		
		$modifier = '';
		if ($amount<=9) {
			$modifier = '00000';
		}else	if($amount>=10 && $amount<=99){
			$modifier = '0000';
		}else if ($amount>=100 && $amount<=999){
			$modifier = '000';
		}else if ($amount>=1000 && $amount<=9999){
			$modifier = '00';
		}else if ($amount>=10000 && $amount<=99999){
			$modifier = '0';
		}else if ($amount>=100000 && $amount<=999999){
			$modifier = '';
		}

		
		return $modifier.$amount.'-'.date('m').date('y');
		
	}



}

?>